package football

import (
	"log"
	"longmen/server/config/global"
	global2 "longmen/server/config/global"
	"longmen/server/internal/services/nami/api/v5/sports/football/football_behaviour"
	"longmen/server/internal/services/nami/config"
	"longmen/server/internal/services/nami/utils/restyclient"
	"longmen/server/pkg/db/mysql/models/models_sports"
	"time"
)

var defineEventID = []int{82, 120, 129, 108, 142, 542, 46, 45} // 82:英超 120:西甲 129:德甲 109:意甲 142:法甲 542:中超 46:欧冠 45:欧洲杯

func SyncCompPoint() {
	for i := 0; i < len(defineEventID); i++ {
		resp := football_behaviour.CompPoint{}
		queryStr := config.BuildParamsAndUserAndSecret(map[string]interface{}{
			"id": defineEventID[i],
		})
		err := restyclient.GetUrlAndBindRecords(config.FBCompPoint+queryStr, &resp)
		if err != nil {
			global2.Log.Error("获取更新列表失败: ", err)
			return
		}

		if len(resp.Results.Tables) > 0 {
			for _, v := range resp.Results.Tables {
				for _, ch := range v.Rows {
					log.Println("resp.Results.Tables.Rows Count:", len(v.Rows))
					log.Println("resp.Results.Tables.Rows:", v.Rows)
					var tempTeam models_sports.FootballTeam
					err := global.Sport().Model(&tempTeam).Where("id=?", ch.TeamID).First(&tempTeam).Error
					if err != nil {
						global2.Log.Error("球队信息不存在，更新数据失败: ", err)
						continue
					}
					r := &models_sports.FootballPoint{
						ID:               ch.TeamID,
						TeamLogo:         tempTeam.Logo,
						TeamName:         tempTeam.NameZh,
						CompID:           defineEventID[i],
						Conference:       v.Conference,
						Group:            v.Group,
						StageID:          v.StageID,
						TeamID:           ch.TeamID,
						PromotionID:      ch.PromotionID,
						Points:           ch.Points,
						Position:         ch.Position,
						DeductPoints:     ch.DeductPoints,
						Note:             ch.Note,
						Total:            ch.Total,
						Won:              ch.Won,
						Draw:             ch.Draw,
						Loss:             ch.Loss,
						Goals:            ch.Goals,
						GoalsAgainst:     ch.GoalsAgainst,
						GoalDiff:         ch.GoalDiff,
						HomePoints:       ch.HomePoints,
						HomePosition:     ch.HomePosition,
						HomeTotal:        ch.HomeTotal,
						HomeWon:          ch.HomeWon,
						HomeDraw:         ch.HomeDraw,
						HomeLoss:         ch.HomeLoss,
						HomeGoals:        ch.HomeGoals,
						HomeGoalsAgainst: ch.HomeGoalsAgainst,
						HomeGoalDiff:     ch.HomeGoalDiff,
						AwayPoints:       ch.AwayPoints,
						AwayPosition:     ch.AwayPosition,
						AwayTotal:        ch.AwayTotal,
						AwayWon:          ch.AwayWon,
						AwayDraw:         ch.AwayDraw,
						AwayLoss:         ch.AwayLoss,
						AwayGoals:        ch.AwayGoals,
						AwayGoalsAgainst: ch.AwayGoalsAgainst,
						AwayGoalDiff:     ch.AwayGoalDiff,
						SeasonID:         resp.Results.SeasonID,
						UpdatedAt:        time.Now(),
						TeamNameEn:       tempTeam.ShortNameEn,
					}
					rows := global2.Sport().Model(&models_sports.FootballPoint{}).Where("id=? and comp_id=?", r.ID, defineEventID[i]).Updates(&r).RowsAffected
					log.Println("team_id rows:", rows)
					if rows == 0 {
						err := global2.Sport().Model(&models_sports.FootballPoint{}).Create(&r).Error
						log.Println("team_id create error:", err)
						if err != nil {
							global2.Log.Error("球队更新数据失败: ", err)
							continue
						}
					}
				}
			}
		}
	}
}
